<?php
App::import('Controller','categories.CmsCategories');
class CategoriesController extends CmsCategoriesController {

	var $name = 'Categories';
	var $helpers = array('Html', 'Form', 'Cms.Index','Rating.rating','Javascript','Modules.section','Categories.Tree','GeoMap');
	var $uses = array('ListCategory');
	
	protected $autoAdminActions = array('admin_bulkDelete');
	
	function view($slug = null)
	{	
		$this->redirect(array("controller"=>"listings","plugin"=>null,'action'=>"index","category","category"=>$slug));
		//all data is now in listing controller.
		/*
		// set action meta info
		$this->actionId = 'categorypages';
		$this->actionParams = array('categorySlug' => $slug);
		
		$this->ListCategory->recursive = -1;
		if(is_numeric($slug))
		{
			$categoryData = $this->ListCategory->findById($slug);
			$this->redirect('/categories/view/'.$categoryData['ListCategory']['slug']);
		}else
		{
			$categoryData = $this->ListCategory->findBySlug($slug);
		}
		//Configure::write('debug', 2);
		$this->pageTitle = __('Categories');		
		if (empty($categoryData))
		{
			$this->errorMsg(__('Invalid Category.', true));
			$this->redirect('/');
		}
		$hideRssFeed = cr('ListingConfig.disable_listing_rss');
		$children = $this->ListCategory->children($categoryData['ListCategory']['id'],true,null,$this->ListCategory->order);
		$categoryPaths = $this->ListCategory->getPath($categoryData['ListCategory']['id'],null,-1);
		$this->set(compact('categoryPaths','categoryData','children','hideRssFeed'));

		$this->pageTitle = $categoryData['ListCategory']['name'];*/
	}

	function admin_index() {
		$this->pageTitle = __('Categories');
		$this->helpers[] = 'Tree';
		$this->paginate['limit'] = 10;
		$this->paginate['recursive'] = 0;
		$data = $this->ListCategory->find('list',array('fields'=>array('id','name')));
		$listCategories = $this->paginate();
		//$listCategories['otherInfo'] = $data;	
		$this->set('listCategories', $listCategories);
		$this->viewVars['categoryInfo'] = $data;		
	}

	function admin_view($id = null) {
		$this->pageTitle = __('View');
		if (!$id) {
			$this->Session->setFlash(__('Invalid Category.', true));
			$this->redirect(array('controller'=>'categories','action'=>'admin_index'));
		}
		$this->set('category', $this->ListCategory->read(null, $id));
		$categoryList = $this->ListCategory->find('list');
		$this->set('categoryList',$categoryList);		
	}

	function admin_add()
	{
		$this->pageTitle = __('Add Category');
		if (!empty($this->data))
		 {
		 	$this->ListCategory->create();
			if ($this->ListCategory->save($this->data))
			{
				$this->successMsg(__('The category has been saved.', true));
				$this->redirect(array('controller'=>'categories','action'=>'admin_index'));
			} else {
				$this->errorMsg(__('The category could not be saved. Please, try again.', true));
			}
		}
		$parentCategoryList = $this->ListCategory->generatetreelist();
		$listingTypes = $this->ListCategory->ListingType->find('list');
		if(!$listingTypes)
		{
			$this->warningMsg(__("Add a listing type first."));
			$this->redirect(array('controller'=>"listing_types",'action'=>"add",'plugin'=>null));
		}
		$this->set(compact('parentCategoryList','listingTypes'));
	}

	function admin_edit($id = null)
	{
		$this->pageTitle = __('Edit Category');
		if (!$id && empty($this->data))
		{
			$this->errorMsg(__('Invalid Category.', true));
			$this->redirect(array('controller'=>'categories','action'=>'admin_index'));
		}
		
		if (!empty($this->data))
		{
			$this->data['ListCategory']['id'] = $id;
			if ($this->ListCategory->save($this->data))
			{
				$this->successMsg(__('The category has been saved.', true));
				$this->redirect(array('controller'=>'categories','action'=>'admin_index'));
			} else {
				$this->errorMsg(__('The category could not be saved. Please, try again.', true));
			}
		}
		
		if (empty($this->data))
		{
			$this->ListCategory->recursive = -1;
			$this->data = $this->ListCategory->read(null, $id);
		}
		$parentCategoryList = $this->ListCategory->generatetreelist();
		$listingTypes = $this->ListCategory->ListingType->find('list');
		$this->set(compact('parentCategoryList','listingTypes'));
	}

	function admin_delete($id = null)
	{
		if (!$id)
		{
			$this->errorMsg(__('Invalid id for category.', true));
			$this->redirect(array('controller'=>'categories','action'=>'admin_index'));
		}
		if ($this->ListCategory->del($id))
		{
			$this->successMsg(__('category deleted.', true));
			$this->redirect(array('controller'=>'categories','action'=>'admin_index'));
		}
	}	
	
	function index()
	{
		$r = $this->ListCategory->getDatasource();
		prd($r);
		if(!$this->Session->read('Auth.User'))
		{
			$this->redirect(array('controller'=>'users','action'=>'login','plugin'=>'users'));	
		}				
	}	

	private function showChildrenTree($parentId=null)
	{
		Configure::write('debug',0);
		$children = $this->Category->children($parentId,true);
		$this->set('children',$children);
	}
		
	function selectCatgeory($parentId=null)
	{
		$category_alias = $this->{$this->modelNames['0']};
		$this->Category = $category_alias;
		Configure::write('debug',0);
		if($parentId=='null')
			$parentId = null;
		else
			$this->layout = 'ajax';
		$this->Category->id = $parentId;
		$this->showChildrenTree($parentId);
		if(isset($this->params['requested']))
			$this->render();
	}	
	
	function admin_updateOrder()
	{
		$this->ListCategory->updateOrder($_POST);
		$this->autoRender = false;
	}
}
?>